package com.example.f22015232.controller;

import com.example.f22015232.model.User;
import com.example.f22015232.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class RegisterController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService; // 确保此处拼写无误

    // 访问注册页面
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    // 处理注册表单提交
    @PostMapping("/register")
    public String register(User user) {
        // 对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));

        // 保存用户到数据库
        userService.save(user);

        // 注册成功后跳转到登录页面
        return "redirect:/login";
    }
}
